package davidtps.godutch.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import davidtps.godutch.activity.R;
import davidtps.godutch.adapter.base.AdapterBase;
import davidtps.godutch.business.BusinessConsume;
import davidtps.godutch.controls.SlideMenuViewItem;
import davidtps.godutch.model.ModelConsume;
import davidtps.godutch.utility.DateTools;

public class AdapterConsumeManagerView extends AdapterBase {
	private BusinessConsume mBusinessConsume;
	private String mAccountId;
	
	private class Holder{
		TextView tvConsumeTime;
		TextView tvConsumeStatistic;
		
		LinearLayout linearLayout;
		ImageView ivConsumeImage;
		TextView tvConsumeCategoryName;
		TextView tvConsumeUserAndMode;
		TextView tvConsumeMoney;
	}
	
	public AdapterConsumeManagerView(Context pContext,String pAccountId) {
		super(pContext, null);
		mBusinessConsume = new BusinessConsume(pContext);
		List<ModelConsume> _ModelConsumes = mBusinessConsume.queryConsumesByAccountId(pAccountId);
		setList(_ModelConsumes);
		mAccountId = pAccountId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder _Holder;
		if(convertView==null){
			convertView = getLayoutInflater().inflate(R.layout.layout_consumerecord_manager_item, null, true);
			
			_Holder = new Holder();
			_Holder.tvConsumeTime = (TextView)convertView.findViewById(R.id.tvConsumeTime);
			_Holder.tvConsumeStatistic = (TextView)convertView.findViewById(R.id.tvConsumeStatistic);
			
			_Holder.linearLayout = (LinearLayout)convertView.findViewById(R.id.lConsumeRecordPerDays);
			
			_Holder.ivConsumeImage = (ImageView)convertView.findViewById(R.id.ivConsumeImage);
			_Holder.tvConsumeCategoryName = (TextView)convertView.findViewById(R.id.tvConsumeCategoryName);
			_Holder.tvConsumeUserAndMode = (TextView)convertView.findViewById(R.id.tvConsumeUserAndMode);
			_Holder.tvConsumeMoney = (TextView)convertView.findViewById(R.id.tvConsumeMoney);
			convertView.setTag(_Holder);
		}else{
			_Holder = (Holder)convertView.getTag();
		}
		ModelConsume _ModelConsume =  (ModelConsume) getList().get(position);
		String _ConsumeDateString = DateTools.getFormatDateTime(_ModelConsume.getConsumeDate(), "yyyy-MM-dd");
		boolean _IsShow = false;
		if(position>0){
			ModelConsume _PreModelConsume =  (ModelConsume) getList().get(position-1);
			String _PreConsumeDateString = DateTools.getFormatDateTime(_PreModelConsume.getConsumeDate(), "yyyy-MM-dd");
			_IsShow = !_PreConsumeDateString.equals(_ConsumeDateString);
		}
		if(_IsShow || position==0){
			_Holder.linearLayout.setVisibility(LinearLayout.VISIBLE);
			String _SummaryMessage = mBusinessConsume.getConsumeSummaryMessage(_ConsumeDateString, mAccountId);
			_Holder.tvConsumeTime.setText(_ConsumeDateString);
			_Holder.tvConsumeStatistic.setText(_SummaryMessage);
		}
		
		
		_Holder.tvConsumeCategoryName.setText(_ModelConsume.getCategoryName());
		_Holder.tvConsumeUserAndMode.setText(mBusinessConsume.getConsumeUserNameByUserId(_ModelConsume.getUserId())+""+_ModelConsume.getConsumeMode());
		_Holder.tvConsumeMoney.setText(_ModelConsume.getConsumeMoney()+"");
		
		return convertView;
	}

}
